import { size, IFormProps, pageSize } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Popconfirm, message } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import AdminEdit from "../../components/users/AdminEdit";

export interface IAdminsPros extends IFormProps {
    user: any
}
class Admins extends React.PureComponent<IAdminsPros, any>{
    constructor(props) {
        super(props);
        this.state = {}
    }

    search = () => {
        this.getAdmins();
    }

    getRoles = () => {
        this.props.dispatch({
            type: "user/getRoles"
        });
    }

    getAdmins = (payload?) => {
        payload = Object.assign({}, this.props.user.adminSearchs, this.props.form.getFieldsValue(), {...payload});
        this.props.dispatch({
            type: "user/getAdmins",
            payload
        });
    }

    reset = () => {
        this.props.form.resetFields();
    }

    edit = (admin = {}) => {
        this.props.dispatch({
            type: "user/save",
            payload: { admin }
        });
        this.setVisible(true);
    }

    setActive = (id, isActive) => {
        this.props.dispatch({
            type: "user/setActive",
            payload: {
                form: { id, isActive },
                callback: () => {
                    this.search();
                }
            }
        })
    }

    resetPassword = (id) => {
        this.props.dispatch({
            type: "user/resetPassword",
            payload: {
                form: { id },
                callback: (password) => {
                    message.info('该用户密码已重置为:' + password);
                }
            }
        })
    }

    remove = (id) => {
        this.props.dispatch({
            type: "user/remove",
            payload: {
                form: { id },
                callback: () => {
                    this.search();
                }
            }
        })
    }

    setVisible = (visible = false) => {
        this.setState({ visible });
    }

    save = (form) => {
        let { admin } = this.props.user;
        form = Object.assign({}, admin, form);
        this.props.dispatch({
            type: "user/saveAdmin",
            payload: {
                form,
                callback: () => {
                    this.setVisible();
                    this.search();
                }
            }
        });
    }

    render(): JSX.Element {
        //console.log(this.props);
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const { admin,
            admins: { totalCount, items },
            adminSearchs: { pageIndex, pageSize, keywords },
            roles
        } = this.props.user;
        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.user.adminSearchs,
                { pageIndex, pageSize });
            this.getAdmins(payload);
        }
        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };

        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                            <Form.Item>
                                <Button size={size} onClick={this.edit}>新增</Button>
                            </Form.Item>
                            <Form.Item>
                                {getFieldDecorator("keywords", {
                                    initialValue: keywords
                                })(<Input
                                    style={{ width: 200 }}
                                    size={size}
                                    placeholder="输入用户名/昵称/手机号"
                                />)}
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>管理员列表</h3>
                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallUsersVOAdminVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return (pageIndex - 1) * pageSize + index + 1
                                }} />
                                <Table.Column title="用户名" key="userName" dataIndex="userName" />
                                <Table.Column title="昵称" key="name" dataIndex="name" />
                                <Table.Column title="角色" key="roleNames" render={
                                    (text, record: api.SingleUserMallUsersVOAdminVO, index) => {
                                        return record.roleNames!.toString();
                                    }} />
                                <Table.Column title="状态" key="isActive" render={(text, record: api.SingleUserMallUsersVOAdminVO, index) => {
                                    return record.isActive ? "启用" : "禁用"
                                }} />
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallUsersVOAdminVO, index) => {
                                    return (
                                        <div>
                                            <a className={Styles.M_RIGHT} onClick={() => this.edit(record)}>
                                                编辑
                                            </a>
                                            {!record.isActive ? (
                                                <a className={Styles.M_RIGHT} onClick={() => this.setActive(record.id, true)}>
                                                    启用
                                            </a>) : (<a className={Styles.M_RIGHT} onClick={() => this.setActive(record.id, false)}>
                                                    禁用
                                            </a>)}
                                            <a className={Styles.M_RIGHT} onClick={() => this.resetPassword(record.id)}>
                                                重置密码
                                            </a>
                                            <Popconfirm title="确定删除吗?" onConfirm={() => { this.remove(record.id); }}>
                                                <a>删除</a>
                                            </Popconfirm>
                                        </div>
                                    );
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
                {this.state.visible ? (
                <AdminEdit visible={this.state.visible}
                admin={admin}
                roles={roles.items}
                onSave={this.save}
                onCancel={this.setVisible} />
        ) : (
          undefined
        )}
            </Row>)
    }
}

export default connect(({ user, loading: { models: { user: loading } } }) => ({ user, loading }))(Form.create()(Admins));
